package com.zilla.android.lib.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
* <一句话功能简述>
* <功能详细描述>
* @author jun.wang
* @version 1.0
* @date 2013-10-30 下午7:54:27
*/
public class MyPopupWindow extends PopupWindow {

	private Context mContext;
	
	private View view;


	
	public MyPopupWindow(View contentView) {
		super(contentView);
		this.view = contentView;
		setMyPopupWindow(contentView);
	}

	public MyPopupWindow(Context context, int layoutID) {
		super();
		this.mContext = context;
		view = View.inflate(mContext, layoutID, null);
		setMyPopupWindow(view);
	}
	
	/**
	* 设置透明的pupupwindow
	* 便于设置带尖角的window 
	*/
	private void setMyPopupWindow(final View view){
		setContentView(view);
	    setFocusable(true);
	    setTouchable(true);
	    setOutsideTouchable(true);
	    setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
	    setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
	    //修复点击背景空白
	    setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	    //设置触摸事件 - 修复触摸弹窗以外的地方无法隐藏弹窗
        view.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
	            final int x = (int) event.getX();
	            final int y = (int) event.getY();
	            
	            if ((event.getAction() == MotionEvent.ACTION_DOWN)
	                    && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >=getHeight()))) {
	            	dismiss();
	                return true;
	            } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
	            	dismiss();
	                return true;
	            } else {
	                return view.onTouchEvent(event);
	            }
			}
		});
	}



}
